#ifndef __APP_H__
#define __APP_H__

#include "baidu_media_play_listener.h"
#include "YTAwake.h"
#include "TulingConfig.h"
#include "TulingAsr.h"
#include "TulingMqtt.h"
#include "TulingReport.h"
#include "TulingUpload.h"
#include "RtosTimer.h"
#include "Airkiss.h"
#include "AirkissCloud.h"
#include "YTRecorderAmr.h"
#include "YTRecorderPcm.h"
#include "TulingBattery.h"

#ifndef CUSTOM_SSID
#define CUSTOM_SSID         ("yte2.4g")
#endif // CUSTOM_SSID

#ifndef CUSTOM_PASSWD
#define CUSTOM_PASSWD       ("yt61600593")
#endif // CUSTOM_PASSWD


class App;
class AppManager;
class AppMediaPlayerListener;

typedef void (AppManager::*MEDIA_SYN_CB_FUN)();//// start callback
typedef void (AppManager::*MEDIA_FIN_CB_FUN)();//// finish callback

class AppMediaPlayerListener : public duer::MediaPlayerListener 
{
public:
    AppMediaPlayerListener(AppManager * _pAppMgr);
    virtual ~AppMediaPlayerListener();
    virtual int on_start();	
	virtual int on_stop();
	virtual int on_finish();
	void SetNextURL(const char* strURL);	
	void SetMediaFinCBFun(MEDIA_FIN_CB_FUN _pFun);
	void SetMediaSynCBFun(MEDIA_SYN_CB_FUN _pFun);
	void SetMediaFinCBFun(MEDIA_FIN_CB_FUN _pFun,bool bEraseFinFun);
	void SetMediaSynCBFun(MEDIA_SYN_CB_FUN _pFun,bool bEraseSynFun);	
	void SetLoop(bool _bLoop);
	void Reset(bool bEraseAll = false);
public:

private:
	char  strLoopURL[256];
	bool bPause;
	bool bLoop;
	char *  strNextURL;
	bool 	bPlayNextURL;
	AppManager	*   pAppMgr;
	MEDIA_FIN_CB_FUN pFinFun;
	MEDIA_SYN_CB_FUN pSynFun;
#if 1
	bool bEraseFinFun;
	bool bEraseSynFun;
#endif
};


class AppManager 
	: public YTRecorderPcm::IOnEvent// recorder
	, public YTRecorderAmr::IOnEvent
	, public YTAwake::IOnEvent		// awake
	, public TulingAsr::IOnEvent	// tuling
	, public TulingMqtt::IOnEvent
	, public TulingReport::IOnEvent
	, public TulingUpload::IOnEvent
	, public Airkiss::IOnEvent
	, public BatteryCheck::IOnEvent
{
public:
	AppManager(App * _pApp);
	~AppManager();	
	void AppStart(bool bNeedNet=true);
	bool AppConnect();
	void AppStop(bool bNeedRec=false);
	/// for wechat
	virtual int on_start_amr();	
	virtual int on_data_amr(const void* data, unsigned int size);	
	virtual int on_stop_amr();
	/// for recorde & engine
	virtual int on_start_pcm();	
	virtual int on_data_pcm(const void* data, unsigned int size);	
	virtual int on_stop_pcm();	
	virtual int on_awake();
	virtual int on_awake_vad_start(short * pStartPart,int nSamples);
	virtual int on_awake_vad_data(const void* data, unsigned int size);
	virtual int on_awake_vad_end();
	/// for airkiss
	virtual int on_airkiss_wait();
	virtual int on_airkiss_connect();
	virtual int on_airkiss_finish();
	/// for turing
	virtual int on_tuling_warning();
	virtual int on_tulling_failed();
	virtual int on_tuling_timeout(int nRet);		
	virtual int on_aiwifi(AiwifiRsp *pRsp);
	virtual int on_begin_recv();
	virtual int on_finish_recv();
	virtual int on_mqtt(MqttContext *pContext);
	virtual int on_report(unsigned char * strReportMsg,int nMsgLen);
	virtual int on_upload(UploadRsp * pUploadRsp);
	/// for bat
	virtual int on_power(int nVal);
	virtual int on_lowpower(int nVal);
#if 1
	void PeekOneWeChatCacheMsg();
#endif
	void aiwifi_syn_cb();
	void aiwifi_fin_cb();		
	void on_awake_key();	
	void WakeTimerCallBack();
	void SleepTimerCallBack();
	void ShutdownTimerCallBack();
#if 1
	void PlayOneLocalData();
#endif
public:
	rtos::RtosTimer yttlSleepTimer;
	rtos::RtosTimer rtAwakeTimer;
	rtos::RtosTimer rtShutdownTimer;

	duer::MediaPlayerListener *pMediaListener;
	bool bTimerCallback;
private:
	App * pApp;
	
	
};


class App
{
public:
    App();
    ~App();
    void start();
    void stop();
	void app_restart_handle();
    void record_start_handle();
    void record_stop_handle();
    void pause_play_press_handle();
	void pause_play_release_handle();
	void voice_up_press_handle();
	void voice_up_release_handle();
	void voice_down_press_handle();
	void voice_down_release_handle();
	void airkiss_press_handle();
	void airkiss_release_handle(); 
	void app_restart_btn_fall();
	void record_btn_fall();
	void record_btn_rise();
	void pause_btn_fall();
	void pause_btn_rise();
	void voice_up_btn_fall();
	void voice_down_btn_fall();
	void voice_up_btn_rise();
	void voice_down_btn_rise();
	void airkiss_btn_fall();
	void airkiss_btn_rise();
	void airkiss_timer_cb();
	void btwifi_timer_cb();
	void voiceup_timer_cb();
	enum CODEC_MODE{MODE_BT,MODE_WIFI,MODE_NONE};
public:
	AppManager		 *pAppMgr;
	rtos::RtosTimer rtAirkissTimer;
	rtos::RtosTimer rtBtWifiTimer;
	rtos::RtosTimer rtVoiceUpTimer;
	CODEC_MODE cmMode;
};


#endif 
